    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sdd.facades.producer;

import java.io.File;
import java.io.FilenameFilter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.Schedule;
import javax.ejb.Stateless;
import javax.ejb.TimerService;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;

/**
 *
 * @author tpgalan
 */
@Stateless
public class ProductProducerFacadeBean implements ProductProducerFacade {

    @Resource(mappedName = "jms/productQueue")
    private Queue productQueue;
    @Resource(mappedName = "jms/productQueueFactory")
    private ConnectionFactory productQueueFactory;
    @Resource
    TimerService timerService;
    private String[] name = {"Color", "Tamaño","velocidad"};
    private String[][] value = {{"Negro", "Blanco", "Gris"}, {"Gigantesco", "mediano", "atómico"},{"Rapido","Lento","Lentito"}};
    private String[] chau = {"Especificación de ", "sinergia  ", "waka waka de ", "esternocleidomastoideo aplicable a ", "tercera forma normal en ", "pectiniforme ", "termolabil "};
    
    private String productos[] ={"Armario","Estanteria","Mueble","Portatil","Movil","Router","Raton","Perro","Gato","Libro"};
    private String productos2[]={"Grande","Chorrada","Rapido","Web","Duro"};
    

    @Override
    @Schedule(minute = "*/1", dayOfMonth = "*", hour = "*", month = "*", year = "*", dayOfWeek = "*")
    public void sendProduct() throws JMSException {
        Session session = null;
        Connection con = null;
        try {
            con = productQueueFactory.createConnection();
            session = con.createSession(true, 0);
            /*Random rand = new Random();
            Integer photo = rand.nextInt(3) + 1;
            File f = new File(".");
            System.out.println(f.getAbsolutePath());
            String[] lis    t = f.list(new MyImageFilter(photo.toString()));
            if (list != null && list.length > 0) {
            FileInputStream fis = null;
            try {
            fis = new FileInputStream(list[0]);
            } catch (FileNotFoundException ex) {
            Logger.getLogger(ProductProducerFacadeBean.class.getName()).log(Level.SEVERE, null, ex);
            }
            byte[] buffer = null;
            try {
            buffer = new byte[fis.available()];
            fis.read(buffer);
            } catch (IOException ex) {
            Logger.getLogger(ProductProducerFacadeBean.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (buffer != null) {*/
            MapMessage map = session.createMapMessage();
            String productname = "";
            productname+=productos[(int) (((int) ((Math.random() * 10000))) % productos.length)];
            productname+=" "+productos2[(int) (((int) ((Math.random() * 100000))) % productos2.length)];
            map.setString("name", productname);
            map.setString("vendor", "Vendor1");
            map.setDouble("price", ((int) (Math.random() * 10000)) / 100);
            map.setString("description", "mucho chowchow");
            map.setString("numberFeat",Integer.toString(name.length));
            for (int row = 0; row < name.length; row++) {
                map.setString("categoryFeature"+Integer.toString(row), this.name[row]);
                map.setString("summary"+Integer.toString(row), this.value[row][(int) (((int) ((Math.random() * 10))) % this.value[0].length)]);
                map.setString("description"+Integer.toString(row), this.chau[(int) (((int) ((Math.random() * 10))) % this.chau.length)] + this.chau[(int) (((int) ((Math.random() * 10))) % this.chau.length)] + this.chau[(int) (((int) ((Math.random() * 10))) % this.chau.length)]);
            }
            //   map.setBytes("image", buffer);
            MessageProducer producer = session.createProducer(productQueue);
            producer.send(map);
            /* }
            
            }*/
        } catch (JMSException ex) {
        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e) {
                    Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot close session", e);
                }
            }
            if (con != null) {
                con.close();
            }
        }
    }
}

class MyImageFilter implements FilenameFilter {

    private String startsWith;

    public MyImageFilter(String startsWith) {
        this.startsWith = startsWith;
    }

    @Override
    public boolean accept(File file, String string) {
        if (string.startsWith(startsWith)) {
            return true;
        }
        return false;
    }
}
